import {Page} from '@/base/Page';
import {deleteAffiliate, getAffiliateList} from '@/base/api/AffiliateApi';
import {FamilyMember} from '@/base/model/FamilyMember';
import {isEmpty} from '@/base/util/Strings';
import {getFamilyMemberList,getMemberInfo,getMemberInfos} from "@/base/api/FamilyApi";
import {FamilyMemberInfo} from '@/base/model/FamilyMemberInfo';

class Form {
  nickname = '';
  createBegin = '';
  createEnd = '';
  joindate = [];
}

export class DetailBloc {
  form = new Form();
  memberInfo = new FamilyMemberInfo();
  paged: Page<FamilyMember> = new Page<FamilyMember>([], 0, 1, 15, 0, []);
  loading = false;
  familyid = 0;
  // isShowDetailModal = false;
  // detailMemberId = 0;

  load(pageIndex: number, pageSize: number) {
    if (!isEmpty(this.form.joindate) && !isEmpty(String(this.form.joindate[0]))) {
      // @ts-ignore
      this.form.createBegin = this.form.joindate[0].getTime() || '';
      // @ts-ignore
      this.form.createEnd = this.form.joindate[1].getTime() + 24 * 60 * 60 * 1000 || '';
    }
    this.loading = true;
    return getFamilyMemberList({
      pageIndex,
      pageSize,
      familyid: this.familyid,
      nickname: this.form.nickname,
      createBegin: this.form.createBegin,
      createEnd: this.form.createEnd
    }).then((res) => {
      this.paged = res.data;
      const firstItem = res.data.items.slice(0, 1);
      // const [firstItem] = res.data.items;
      if (firstItem !== undefined) {
        getMemberInfos({
          memberId: firstItem[0].memberId,
          infoAll: 1
        })
        .then((res) => {
          this.memberInfo = res.data;
        });
      }
    }).finally(() => {
      this.loading = false;
    });
  }

  // removeItem(dateItem: Affiliate) {
  //   //   this.loading = true;
  //   //   return deleteAffiliate(dateItem.id)
  //   //     .finally(() => {
  //   //       this.loading = false;
  //   //     });
  //   // }

  reset() {
    this.form = new Form();
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
  }

  // changeDetailModal(id: number) {
  //   this.isShowDetailModal = !this.isShowDetailModal;
  //   this.detailMemberId = id;
  //   if (!this.isShowDetailModal) {
  //     this.load(this.paged.pageIndex, this.paged.pageSize);
  //   }
  // }

}
